<template>
    <div class="monitor-page">
        <a-card title="小区监控系统" style="width: 100%; max-width: 1200px; margin: 0 auto;">
            <div class="monitor-grid">
                <div v-for="(camera, index) in cameras" :key="index" class="monitor-item">
                    <div class="monitor-header">
                        <span class="camera-name">{{ camera.name }}</span>
                    </div>
                    <div class="monitor-content">
                        <video v-if="camera.type === 'stream'" :id="'video-' + index" autoplay muted
                            class="video-player"></video>
                    </div>
                </div>
            </div>
        </a-card>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import Hls from 'hls.js';

const cameras = ref([
    {
        name: "小区大门",
        url: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
        type: "stream"
    },
    {
        name: "中心花园",
        url: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
        type: "stream"
    },
    {
        name: "小区侧门",
        url: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
        type: "stream"
    },
    {
        name: "地下车库",
        url: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
        type: "stream"
    }
]);

let hlsInstances = [];

onMounted(() => {
    // 遍历所有摄像头
    cameras.value.forEach((camera, index) => {
        if (camera.type === 'stream') {
            const video = document.getElementById(`video-${index}`); // 获取视频元素,第一个摄像头 index = 0，获取 video-0 元素
            const hls = new Hls(); // 创建 HLS 播放器实例
            hls.loadSource(camera.url); // 加载 HLS 源
            hls.attachMedia(video); // 将 HLS 播放器实例附加到视频元素,将 HLS 视频流和 video 元素连接起来
            hlsInstances.push(hls); // 保存 HLS 实例
        }
    });
});

onUnmounted(() => {
    // 清理 HLS 实例
    hlsInstances.forEach(hls => {
        hls.destroy();
    });
});
</script>

<style scoped>
.monitor-page {
    min-height: 100vh;
    padding: 20px;
    background: #f0f2f5;
}

.monitor-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    padding: 12px;
    height: calc(100vh - 180px);
}

.monitor-item {
    border: 1px solid #f0f0f0;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
}

.monitor-header {
    padding: 6px 12px;
    background: #fafafa;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
}

.camera-name {
    font-weight: 500;
    font-size: 14px;
}

.monitor-content {
    position: relative;
    flex: 1;
    background: #000;
    min-height: 0;
}

.video-player {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
    .monitor-grid {
        grid-template-columns: 1fr;
        height: calc(100vh - 160px);
    }
}
</style>